package com.pub.functions;

public class BigEndianFuncs {
	public static byte[] toByteArray(int iSource, int iArrayLen) {
		byte[] bLocalArr = new byte[iArrayLen];
		for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
			bLocalArr[iArrayLen - i - 1] = (byte) (iSource >> 8 * i & 0xFF);
		}
		return bLocalArr;
	}

	public static byte[] toByteArray(long iSource, int iArrayLen) {
		byte[] bLocalArr = new byte[iArrayLen];
		for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
			bLocalArr[iArrayLen - i - 1] = (byte) (iSource >> 8 * i & 0xFF);

		}
		return bLocalArr;
	}

	public static int toInt(byte[] bRefArr) {
		int iOutcome = 0;
		byte bLoop;

		if (bRefArr == null) {
			return 0;
		}

		for (int i = 0; i < bRefArr.length; i++) {
			bLoop = bRefArr[i];
			iOutcome <<= 8;
			iOutcome += bLoop & 0xFF;
		}

		return iOutcome;
	}

	public static long toLong(byte[] bRefArr) {
		long iOutcome = 0;
		byte bLoop;

		if (bRefArr == null) {
			return 0;
		}

		for (int i = 0; i < bRefArr.length; i++) {
			bLoop = bRefArr[i];
			iOutcome <<= 8;
			iOutcome += bLoop & 0xFF;
		}
		return iOutcome;
	}

	public static long toLong(byte[] bRefArr, int len) {
		long iOutcome = 0;
		byte bLoop;

		if (bRefArr == null) {
			return 0;
		}

		for (int i = 0, j = 0; i < bRefArr.length && j < len; i++, j++) {
			bLoop = bRefArr[i];
			iOutcome <<= 8;
			iOutcome += bLoop & 0xFF;
		}
		return iOutcome;
	}
}
